package com.ljm.generator.util;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ChenHuaMing
 * 代码生成工具类
 */
public class CodeGenerator {
    public static void generator(CodeConf codeConf) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(codeConf.outputDir);
        gc.setAuthor(codeConf.getAuthor());
        gc.setOpen(true);
        gc.setSwagger2(true);
        gc.setFileOverride(true);
        mpg.setGlobalConfig(gc);
        // 切换为 freemarker 模板引擎
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(codeConf.getDbUrl());
        dsc.setDriverName(codeConf.getDbDriverName());
        dsc.setUsername(codeConf.getDbUserName());
        dsc.setPassword(codeConf.getDbPassword());
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();

        pc.setParent(codeConf.getPgParent());
        pc.setModuleName(codeConf.getPgModuleName());

        String[] tableName = codeConf.tableNames;

        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("dtoPackage", pc.getParent() + ".dto");
                map.put("apiPackage", pc.getParent() + ".api");
                this.setMap(map);
            }
        };
        ArrayList<FileOutConfig> focList = new ArrayList<>();
        //dto模版配置
        focList.add(new FileOutConfig("/templates/generator/dto.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return gc.getOutputDir() + "//"
                        + pc.getParent().replace(".", "//")
                        + "//" + "dto" + "//"
                        + tableInfo.getEntityName() + "Dto" + StringPool.DOT_JAVA;
            }
        });
        //api模版配置
        focList.add(new FileOutConfig("/templates/generator/api.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return gc.getOutputDir() + "//"
                        + pc.getParent().replace(".", "//")
                        + "//" + "api" + "//"
                        + tableInfo.getEntityName() + "Api" + StringPool.DOT_JAVA;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController("/templates/generator/controller.java");
        templateConfig.setServiceImpl("/templates/generator/serviceImpl.java");

        mpg.setTemplate(templateConfig);

        // 策略配置isDelete
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(false);
        strategy.setLogicDeleteFieldName("is_delete");
        strategy.setVersionFieldName("version");
        strategy.setInclude(tableName);
        strategy.setControllerMappingHyphenStyle(false);
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }
    @Builder
    @Data
    static class CodeConf{
        private String outputDir;
        private String author;

        private String dbUrl;
        private String dbDriverName;
        private String dbUserName;
        private String dbPassword;

        private String pgParent;
        private String pgModuleName;
        private String[] tableNames;
    }
}
